<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 10.2.&nbsp; Templates</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-10-SECT-1.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-10-SECT-3.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="learnphpmysql-CHP-10-SECT-2"></a>
<h3 id="title-IDAHXG5P" class="docSection1Title">10.2. Templates</h3>
<p class="docText">Templates separate the HTML code that defines the presentation or look of a page from the PHP code that's responsible for gathering the data. Once separated, it becomes easier for someone with HTML and perhaps CSS knowledge to modify the template without worrying about breaking the PHP code. Likewise, the PHP code can focus on the data instead of getting caught up in presentation details.</p>
<p class="docText">There are other advantages to using templates,<a name="IDX-CHP-10-0495"></a> 
<a name="IDX-CHP-10-0496"></a> 
 too. If you make a mistake in the template, the error will be clearly returned from the template. The template itself can generally be loaded into a web browser or a graphical web development tool such as Dreamweaver, since it resembles the final state of the page when processed. Templates support very basic programming features for use with presentation, such as being able to tell whether a section of a page should be visible.</p>
<p class="docText">Of course, nothing's perfect; there are a couple of disadvantages to templates. Templates increase the number of files to maintain. They add a small amount of extra processing time. They also require installing the template engine<a name="IDX-CHP-10-0497"></a> 
<a name="IDX-CHP-10-0498"></a> 
 and setting up directories. You need to be running at least PHP Version 4.0.6 to use Smarty, a popular template engine.</p>
<a name="learnphpmysql-CHP-10-SECT-2.1"></a>
<h4 id="title-IDAMYG5P" class="docSection2Title">10.2.1. Template Engine</h4>
<p class="docText">There are several template packages available on the Internet. Each uses its own <span class="docEmphasis">template engine</span> to process the templates and make them as efficient as possible. No matter which template engine you use, you'll always follow the same basic steps:</p>
<div style="font-weight:bold"><ol class="docList" type="1"><li><div style="font-weight:normal"><p class="docList">Retrieve your data.</p></div></li><li><div style="font-weight:normal"><p class="docList">Make calls to the template functions for each value that's used in a template.</P></div></li><li><div style="font-weight:normal"><p class="docList">Display the template using the <tt>template</tt> function.</P></div></LI></ol></div>
<p class="docText">We'll walk through this process with some examples shortly. One of the more popular template engines available is Smarty,<a name="IDX-CHP-10-0499"></a> 
 shown in <a class="docLink" href="#learnphpmysql-CHP-10-FIG-16">Figure 10-16</a>. Smarty has many, many features, but what we're concerned most with is the basic template engine functionality.</p>
<a name="learnphpmysql-CHP-10-FIG-16"></a><P><center>
<h5 class="docFigureTitle">Figure 10-16. Smarty template engine</h5>
<img border="0" alt="" width="86" height="29" SRC="images/learnphpmysql_1016.jpg">
</center></p><BR>

<a name="learnphpmysql-CHP-10-SECT-2.2"></a>
<h4 id="title-IDAB0G5P" class="docSection2Title">10.2.2. Installation</h4>
<a name="IDX-CHP-10-0500"></a> 
<a name="IDX-CHP-10-0501"></a> 
<a name="IDX-CHP-10-0502"></a> 

<p class="docText">While installing Smarty isn't as complex as installing and configuring Apache, PHP, and MySQL, it still deserves some attention.</P>
<div style="font-weight:bold"><ol class="docList" type="1"><li><div style="font-weight:normal"><p class="docList">Smarty can be downloaded from <a class="docLink" target="_blank" href="http://smarty.php.net/download.php">http://smarty.php.net/download.php</a>. Download the latest stable release.</p></div></LI><li><div style="font-weight:normal"><p class="docList">Extract the contents of the Smarty file to a convenient location.</p></div></li><li><div style="font-weight:normal"><p class="docList">Create a directory called <span class="docEmphasis">Smarty</span> in your document root. If you don't know what your document root is, you can use PHP to find out:</P><pre>
&lt;?php
echo $_SERVER["DOCUMENT_ROOT"];
?&gt;
</pre><BR>
</div></li><li><div style="font-weight:normal"><p class="docList">Copy the contents of Smarty's <span class="docEmphasis">libs/</span> directory from the directory you extracted it to into the Smarty directory you just created.</p></div></li><LI><div style="font-weight:normal"><p class="docList">You should now have the following file structure in your document root:</p><blockquote><P><p class="docList"><span class="docEmphasis">Smarty/Config_File.class.php</span></p></P><P><p class="docList"><span class="docEmphasis">Smarty/debug.tpl</span></P></p><p><p class="docList"><span class="docEmphasis">Smarty/internals/</span></p></p><p><p class="docList"><span class="docEmphasis">Smarty/plugins/</span></p></p><p><p class="docList"><span class="docEmphasis">Smarty/Smarty.class.php</span></p></p><p><p class="docList"><span class="docEmphasis">Smarty/Smarty_Compiler.class.php</span></p></p></blockquote></div></li></ol></div>
<a name="learnphpmysql-CHP-10-SECT-2.2.1"></a>
<H5 id="title-IDAW2G5P" class="docSection3Title">10.2.2.1. Application level directories</h5>
<p class="docText">For each application with which you wish to use Smarty, you'll need to set up a set of four directories. The four directories are for templates, compiled templates, cached templates, and configuration files. Although you may not use all of those features, you should set up the directories just in case.</p>
<div style="font-weight:bold"><ol class="docList" type="1"><LI><div style="font-weight:normal"><p class="docList">Create a directory called <span class="docEmphasis">myapp/</span> in your document root. (You can call it whatever you want, but for the remainder of the text, we will refer to it as <span class="docEmphasis">myapp/</span>.)</P></div></li><LI><div style="font-weight:normal"><p class="docList">Create a directory named <span class="docEmphasis">smarty</span> inside the directory you just created (<span class="docEmphasis">myapp/smarty</span>).</p></div></li><li><div style="font-weight:normal"><p class="docList">In the <span class="docEmphasis">smarty</span> directory you just created, create four more directories: <span class="docEmphasis">templates, templates_c, cache</span>, and <span class="docEmphasis">config</span>. Ensure that the web server will have write access to the <span class="docEmphasis">templates_c</span> and <span class="docEmphasis">cache</span> directories that you created in the previous step.</P></div></li></ol></div>
<p class="docText">All you need to do is create a template and a PHP file to try it out.</p>

<a name="learnphpmysql-CHP-10-SECT-2.2.2"></a>
<H5 id="title-IDAD4G5P" class="docSection3Title">10.2.2.2. Creating sample scripts</h5>
<p class="docText">Now set up your application in the document root. See <a class="docLink" href="#learnphpmysql-CHP-10-EX-11">Example 10-11</a>.</p>
<a name="learnphpmysql-CHP-10-EX-11"></a><H5 id="title-IDAR4G5P" class="docExampleTitle">Example 10-11. The index.php file to create</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
?php
// use the absolute path for Smarty.class.php
require($_SERVER["DOCUMENT_ROOT"].'/Smarty/Smarty.class.php');
$smarty = new Smarty();
$smarty-&gt;template_dir = $_SERVER["DOCUMENT_ROOT"].'/myapp/smarty/templates';
$smarty-&gt;compile_dir = $_SERVER["DOCUMENT_ROOT"].'/myapp/smarty/templates_c';
$smarty-&gt;cache_dir = $_SERVER["DOCUMENT_ROOT"].'/myapp/smarty/cache';
$smarty-&gt;config_dir = $_SERVER["DOCUMENT_ROOT"].'/myapp/smarty/configs';
?&gt;
</pre><BR>

</TD></tr></table></p>
<p class="docText">The bulk of what's happening in <a class="docLink" href="#learnphpmysql-CHP-10-EX-11">Example 10-11</a> is telling your PHP program where to find the Smarty class file to include and the location of the application directories.</p>
<p class="docText">Next, create <span class="docEmphasis">myapp/index.php</span>:</p>
<pre>
&lt;?php
require_once("smarty.php");
$smarty-&gt;assign('test', '123');
$smarty-&gt;display('index.tpl');
?&gt;
</pre><BR>


<a name="learnphpmysql-CHP-10-SECT-2.2.3"></a>
<h5 id="title-IDAK5G5P" class="docSection3Title">10.2.2.3. Create a sample template</H5>
<p class="docText">Edit the <span class="docEmphasis">index.tpl</span> file in your <span class="docEmphasis">myapp/smarty/templates</span> directory, as shown in <a class="docLink" href="#learnphpmysql-CHP-10-EX-12">Example 10-12</a>.</p>
<a name="learnphpmysql-CHP-10-EX-12"></a><H5 id="title-IDA25G5P" class="docExampleTitle">Example 10-12. The sample index.tpl template to create</H5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Smarty&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
It's as easy as {$test}.
&lt;/body&gt;
&lt;/html&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">Now, go to your new application through the web browser (<a class="docLink" target="_blank" href="http://www.domain.com/myapp/index.php">http://www.domain.com/myapp/index.php</a>, in our example). You should see something like <a class="docLink" href="#learnphpmysql-CHP-10-FIG-17">Figure 10-17</a>.</p>
<a name="learnphpmysql-CHP-10-FIG-17"></a><p><center>
<h5 class="docFigureTitle">Figure 10-17. Web browser-displayed code</h5>
<img border="0" alt="" width="549" height="148" SRC="images/learnphpmysql_1017.jpg">
</center></p><br>
<p class="docText">Now you can convert the previous example to the version shown in <a class="docLink" href="#learnphpmysql-CHP-10-EX-13">Example 10-13</a>.</p>
<a name="learnphpmysql-CHP-10-EX-13"></a><h5 id="title-IDAIBH5P" class="docExampleTitle">Example 10-13. Using the template to display the table</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
&lt;?php
function query_db($qstring){
require_once("smarty.php");
require_once("db_login.php");
require_once("DB.php");
$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if (DB::isError($connection)){
die("Could not connect to the database: &lt;br /&gt;". DB::errorMessage($connection));
}
$query = "SELECT * FROM `books`
NATURAL JOIN `authors`
WHERE `books`.`title` like '%$qstring%'";
$result = $connection-&gt;query($query);
if (DB::isError($result)){
die ("Could not query the database: &lt;br&gt;". $query. " ".DB::errorMessage($result));
}
while ($result_row = $result-&gt;fetchRow(DB_FETCHMODE_ASSOC)) {
$test[] = $result_row;
}
$connection-&gt;disconnect();
$smarty-&gt;assign('users', $test);
$smarty-&gt;display('index2.tpl');
}
?&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Building a Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
$search = $_GET["search"];
$self = $_SERVER['PHP_SELF'];
if ($search != NULL){
echo "The search string is: &lt;strong&gt;$search&lt;/strong&gt;.";
query_db($search);
}
else {
echo '
&lt;form action="'.$self.'" method="GET"&gt;
&lt;label&gt;
Search:
&lt;input type="text" name="search" id="search" /&gt;
&lt;/label&gt;
&lt;input type="submit" value="Go!"&gt;
&lt;/form&gt;';
}
?&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><BR>

</td></TR></table></p>
<p class="docText">The template is a bit more complex, since you're dealing with rows coming back in arrays and there are multiple rows.</p>
<p class="docText">The <span class="docEmphasis">index2.tpl</span> file is shown in <a class="docLink" href="#learnphpmysql-CHP-10-EX-14">Example 10-14</a>.</p>
<a name="learnphpmysql-CHP-10-EX-14"></a><H5 id="title-IDA3BH5P" class="docExampleTitle">Example 10-14. The new table template</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
&lt;table border=1&gt;
&lt;tr&gt;&lt;th&gt;Title&lt;/th&gt;&lt;th&gt;Author&lt;/th&gt;&lt;th&gt;Pages&lt;/th&gt;&lt;/tr&gt;
{section name=mysec loop=$users}
  {strip}
  &lt;tr&gt;
    &lt;td&gt;{$users[mysec].title}&lt;/td&gt;
    &lt;td&gt;{$users[mysec].author}&lt;/td&gt;
    &lt;td&gt;{$users[mysec].pages}&lt;/td&gt;
  &lt;/tr&gt;
  {/strip}
{/section}
&lt;/table&gt;
</pre><br>

</TD></tr></table></p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-10-EX-14">Example 10-14</a> outputs the screen shown in <a class="docLink" href="#learnphpmysql-CHP-10-FIG-18">Figure 10-18</a>.</p>
<a name="learnphpmysql-CHP-10-FIG-18"></a><p><center>
<H5 class="docFigureTitle">Figure 10-18. The output doesn't change even though you're using the template</H5>
<img border="0" alt="" width="549" height="170" SRC="images/learnphpmysql_1018.jpg">
</center></p><br>
<p class="docText">The template incorporates the looping element of Smarty. We used an associative array for returning your results to make the template easier to read, as the field names are the column names and not numbers. Smarty could have easily added some nice decorations, such as alternating the color of the row backgrounds.</p>
<p class="docText">In the next chapter, we'll discuss more complicated database functions now that you have a good solid understanding of database functions.</p>



<a href="11011536.html"><img src="images/pixel.jpg" alt="" width="1" height="1" border="0"></a></TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-10-SECT-1.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-10-SECT-3.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
